********************************************************************************
* 00_master.do
* Master Script for JHR Replication Package
* "Teacher Testing Standards and the New Teacher Pipeline"
* Law, Marks, and Stern
*
* Runs all Stata .do files in sequence to produce:
*   - Cleaned ETS treatment data
*   - Descriptive Tables 1, 2, 3
*   - Main regression Tables 4, 5, 6, 7, A1, A2
*   - Secondary event study regressions (Figures 5-7)
*   - All figures (1-7, A2, A3)
*
* Prerequisites:
*   - Stata 16+ with reghdfe, ftools, outreg2, coefplot, estout
*   - data/raw/ets/*.dta and *.xlsx files
*   - data/cleaned/*.xlsx files (from IPEDS cleaning — run R scripts first)
*
* Usage:
*   1. Open Stata
*   2. cd "SET_YOUR_PATH_HERE/Clean"
*   3. do code/00_master.do
*
* Estimated runtime: ~4-6 hours (mostly bootstrap regressions)
********************************************************************************

clear all
set more off
set matsize 5000
set maxvar 10000

* ──────────────────────────────────────────────────────────────────────────────
* Set working directory
* ──────────────────────────────────────────────────────────────────────────────

* TODO: Set this to the absolute path of the Clean/ directory on your system
cd "SET_YOUR_PATH_HERE/Clean"

display ""
display "================================================================"
display "  JHR Replication Package — Stata Master Script"
display "  Teacher Testing Standards and the New Teacher Pipeline"
display "  Law, Marks, and Stern"
display "================================================================"
display ""
display "  Working directory: `c(pwd)'"
display "  Stata version: `c(stata_version)'"
display "  Date: `c(current_date)' `c(current_time)'"
display ""

* ──────────────────────────────────────────────────────────────────────────────
* Install required packages
* ──────────────────────────────────────────────────────────────────────────────

display "Checking required packages..."

foreach pkg in reghdfe ftools outreg2 coefplot estout {
    capture which `pkg'
    if _rc {
        display "  Installing `pkg'..."
        ssc install `pkg', replace
    }
    else {
        display "  `pkg' — installed"
    }
}

* reghdfe needs ftools registered
reghdfe, compile

display ""

* ──────────────────────────────────────────────────────────────────────────────
* Create output directories
* ──────────────────────────────────────────────────────────────────────────────

capture mkdir "output"
capture mkdir "output/tables"
capture mkdir "output/figures"
capture mkdir "data/cleaned"

* ──────────────────────────────────────────────────────────────────────────────
* Start log
* ──────────────────────────────────────────────────────────────────────────────

capture log close _all
log using "output/replication_log.txt", replace text

timer clear
timer on 1

* ══════════════════════════════════════════════════════════════════════════════
* STEP 1: Clean ETS Data
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "================================================================"
display "  STEP 1/6: Clean ETS Data"
display "================================================================"

timer on 2
do "code/01_clean_ets_data.do"
timer off 2

display ""
quietly timer list 2
display "  Step 1 completed in " %6.1f r(t2)/60 " minutes"

* ══════════════════════════════════════════════════════════════════════════════
* STEP 2: Merge ETS Treatment + IPEDS Data
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "================================================================"
display "  STEP 2/6: Merge ETS Treatment + IPEDS Data"
display "================================================================"

timer on 7
do "code/04_merge_event_data.do"
timer off 7

display ""
quietly timer list 7
display "  Step 2 completed in " %6.1f r(t7)/60 " minutes"

* ══════════════════════════════════════════════════════════════════════════════
* STEP 3: Create Descriptive Tables
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "================================================================"
display "  STEP 3/6: Create Descriptive Tables 1, 2, 3"
display "================================================================"

timer on 3
do "code/03_create_descriptive_tables.do"
timer off 3

display ""
quietly timer list 3
display "  Step 3 completed in " %6.1f r(t3)/60 " minutes"

* ══════════════════════════════════════════════════════════════════════════════
* STEP 4: Secondary Regressions (Figures 5-7)
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "================================================================"
display "  STEP 4/6: Secondary Regressions (Figures 5-7)"
display "================================================================"

timer on 4
do "code/05_secondary_regressions.do"
timer off 4

display ""
quietly timer list 4
display "  Step 4 completed in " %6.1f r(t4)/60 " minutes"

* ══════════════════════════════════════════════════════════════════════════════
* STEP 5: Main Regressions (Tables 4-7, A1, A2)
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "================================================================"
display "  STEP 5/6: Main Regressions (Tables 4-7, A1, A2)"
display "  NOTE: This is the longest step (~3-5 hours with bootstrap)"
display "================================================================"

timer on 5
do "code/06_main_regressions.do"
timer off 5

display ""
quietly timer list 5
display "  Step 5 completed in " %6.1f r(t5)/60 " minutes"

* ══════════════════════════════════════════════════════════════════════════════
* STEP 6: Generate Figures
* ══════════════════════════════════════════════════════════════════════════════

display ""
display "================================================================"
display "  STEP 6/6: Generate Figures"
display "================================================================"

timer on 6
do "code/07_figures.do"
timer off 6

display ""
quietly timer list 6
display "  Step 6 completed in " %6.1f r(t6)/60 " minutes"

* ══════════════════════════════════════════════════════════════════════════════
* SUMMARY
* ══════════════════════════════════════════════════════════════════════════════

timer off 1

display ""
display "================================================================"
display "  REPLICATION COMPLETE"
display "================================================================"
display ""
display "  Output files:"
display "    Tables:"
display "      - table_1_state_info.xlsx"
display "      - table_2_summary_statistics.xlsx"
display "      - table_3_ppst_zscores.xlsx"
display "      - table_4_enrollments.xls"
display "      - table_5_graduations.xls"
display "      - table_6_enrollments.xls / table_6_graduations.xls"
display "      - table_7_enrollments.xls / table_7_completers.xls"
display "      - table_A1_enrollments.xls / table_A1_graduations.xls"
display "      - table_A2_*_event_study.xls"
display "      - composite_enrollments_event_study_total.csv"
display "      - composite_graduations_event_study_total.csv"
display "      - placebo_*.csv, state_licenses_*.csv, teacher_shortage_*.csv"
display ""
display "    Figures:"
display "      - figure_1_tdi_over_time.pdf/.png"
display "      - figure_2a_scatter_enrollments.pdf/.png"
display "      - figure_2b_scatter_graduations.pdf/.png"
display "      - figure_3_enrollments_event_study.pdf/.png"
display "      - figure_4_graduations_event_study.pdf/.png"
display "      - figure_5a_placebo_enrollments.pdf/.png"
display "      - figure_5b_placebo_graduations.pdf/.png"
display "      - figure_5c_other_education_graduations.pdf/.png"
display "      - figure_6_licenses_event_study.pdf/.png"
display "      - figure_7_shortages_event_study.pdf/.png"
display "      - figure_a2_titleII_graduations.pdf/.png"
display "      - figure_a3_scatter_licenses.pdf/.png"
display ""

quietly timer list 1
display "  Total runtime: " %6.1f r(t1)/60 " minutes (" %6.1f r(t1)/3600 " hours)"
display ""
display "  Log saved to: output/replication_log.txt"
display ""

log close
